﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace dmp_ui_demo
{
    /// <summary>
    /// 编辑器中的段落
    /// </summary>
    [Serializable]
    public class Paragraph : ICloneable
    {
        private ParagraphList children;
        private string title;
        private string content;
        private Article article;

        /// <summary>
        /// 段落的唯一标记
        /// </summary>
        [XmlIgnore]
        public object Identity { get; set; }

        /// <summary>
        /// 父级段落
        /// </summary>
        [XmlIgnore]
        public Paragraph Parent { get; internal set; }

        /// <summary>
        /// 高亮关键字后的内容
        /// </summary>
        [XmlIgnore]
        public string HighlightContent { get; set; }

        [XmlIgnore]
        public int Level
        {
            get
            {
                if (this.Parent == null)
                {
                    return 0;
                }
                return this.Parent.Level + 1;
            }
        }

        /// <summary>
        /// 段落标题
        /// </summary>
        public string Title
        {
            set
            {
                title = value;
            }
            get
            {
                return title ?? string.Empty;
            }
        }

        /// <summary>
        /// 段落内容
        /// </summary>
        public string Content
        {
            set
            {
                content = value;
            }
            get
            {
                return content ?? string.Empty;
            }
        }

        /// <summary>
        /// 子段落
        /// </summary>
        public ParagraphList Children
        {
            get
            {
                if (this.children == null)
                {
                    this.children = new ParagraphList(this);
                }
                return this.children;
            }
        }

        [XmlIgnore]
        public Article Article
        {
            get
            {
                if (this.article == null)
                {
                    this.article = this.FindArticle();
                }
                return this.article;
            }
        }

        public Paragraph()
        {
            Identity = Guid.NewGuid().ToString();
        }
        public Paragraph(Article article)
            : this()
        {
            this.article = article;
        }

        internal void AddToArticle()
        {
            if (this.Article != null)
            {
                this.Article.allParagraph.Add(this.Identity, this);
                if (this.children != null && this.children.Count > 0)
                {
                    foreach (var child in children)
                    {
                        child.AddToArticle();
                    }
                }
            }
        }

        private Article FindArticle()
        {
            Paragraph paragraph = this;
            while (paragraph.Parent != null)
            {
                paragraph = paragraph.Parent;
            }
            return paragraph.article;
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

    }
}
